﻿using HDSmartCommunity.BUS;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthorizeController : ControllerBase
    {
        public AuthorizeController()
        {
        }

        /// <summary>
        /// 授权
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [HttpGet]
        [Route("SwitchingMode")]
        public ResponseMessage SwitchingMode(int devid, int mode)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                if (mode == 1)
                {
                    //LogHelper.Log("退出梯控");
                    serial.HDTK(1, "超级管理员手动切换");
                }
                else if (mode == 0)
                {
                    //LogHelper.Log("进入梯控");
                    serial.HDTK(0, "超级管理员手动切换");
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "失败";

                LogHelper.Log("梯控切换失败" + ex.ToString());
            }

            return message;
        }

        /// <summary>
        /// 查询当前模式
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [HttpGet]
        [Route("QueryMode")]
        public ResponseMessage QueryMode()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                DevInfo dev = serial.GetDevInfo();
                message.errcode = 0;
                message.data = dev;
                message.total = 1;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询失败";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 开门保持
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("KeepOpen")]
        public ResponseMessage KeepOpen(int value)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                bool result = serial.KeepOpen(value);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "切换开门保持失败";
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "切换开门保持异常";
            }

            return message;
        }

        /// <summary>
        /// 开门保持
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryKeepOpen")]
        public ResponseMessage QueryKeepOpen()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                message.errcode = 0;
                message.data = serial.QueryKeepOpen();
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "查询开门保持异常";
            }

            return message;
        }
    }
}
